package com.itbour.service.yjx.trade.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.common.util.StringUtils;
import com.itbour.dao.yjx.trade.YjxOrdersDao;
import com.itbour.dto.trade.OrderDetailInfoDto;
import com.itbour.dto.trade.OrderInfoDto;
import com.itbour.dto.trade.PartnerInfoDto;
import com.itbour.dto.trade.StateHisInfoDto;
import com.itbour.dto.yjx.trade.YjxUserPointsHis;
import com.itbour.service.yjx.trade.YjxOrdersService;


// 一键秀 分类管理
public class YjxOrdersServiceImpl implements YjxOrdersService {

	private YjxOrdersDao yjxOrderDao;
	
	
	public Page ordersPgQuery(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = yjxOrderDao.selectOrdersCnt(param);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<OrderInfoDto> data = yjxOrderDao.selectOrdersInfo(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}

	public List<OrderInfoDto> selectOrdersInfo(Map<String,Object> param) {
		return yjxOrderDao.selectOrdersInfo(param);
	}
	
	public void delOrder(String delIds) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("order_ids", StringUtils.listToString2(StringUtils.stringToList2(delIds)));
		yjxOrderDao.deleteOrderInfoLogic(param);
		yjxOrderDao.deleteDetailInfoLogic(param);// 一并删除明细信息
	}

	public void addOrder(OrderInfoDto orderInfo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("order_no", orderInfo.getOrder_no());
		param.put("inv_id", orderInfo.getInv_id());
		param.put("partner_id", orderInfo.getPartner_id());
		param.put("pay_platform", orderInfo.getPay_platform());
		param.put("desc", orderInfo.getDesc());
		param.put("state", orderInfo.getState());
		param.put("total_fee", orderInfo.getTotal_fee());
		param.put("addr_id", orderInfo.getAddr_id());
		param.put("user_id", orderInfo.getUser_id());
		param.put("update_by", orderInfo.getUpdate_by());
		yjxOrderDao.insertOrderInfo(param);
	}
	
	
	// 明细列表
	public Page detailsPgQuery(Map<String,Object> param, Integer pageNo, Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = yjxOrderDao.selectDetailsCnt(param);
			
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<OrderDetailInfoDto> data = yjxOrderDao.selectDetailsInfo(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}

	public List<OrderDetailInfoDto> selectDetailsInfo(Map<String,Object> param) {
		return yjxOrderDao.selectDetailsInfo(param);
	}
	
	public void delDetail(String delIds) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids", StringUtils.listToString2(StringUtils.stringToList2(delIds)));
		yjxOrderDao.deleteDetailInfoLogic(param);
	}

	public void addDetail(OrderDetailInfoDto detailInfo) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("order_id", detailInfo.getOrder_id());
		param.put("item_id", detailInfo.getItem_id());
		param.put("pp_id", detailInfo.getPp_id());
		param.put("unit", detailInfo.getUnit());
		param.put("qty", detailInfo.getQty());
		param.put("user_doc_id", detailInfo.getUser_doc_id());
		param.put("state", detailInfo.getState());
		param.put("fee", detailInfo.getFee());
		param.put("desc", detailInfo.getDesc());
		param.put("update_by", detailInfo.getUpdate_by());
		yjxOrderDao.insertDetailInfo(param);
	}
	
	public void updDetail(Map<String,Object> param) {
		yjxOrderDao.updateDetailInfoById(param);
	}
	
	public void updOrder(Map<String,Object> param) {
		yjxOrderDao.updateOrderInfoById(param);
	}

	public List<PartnerInfoDto> selectPartnerInfo(Map<String,Object> param){
		return yjxOrderDao.selectPartnerInfo(param);
	}
	
	// 订单状态历史
	public void addStateHis(StateHisInfoDto stateHisInfo){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("trade_obj_id", stateHisInfo.getTrade_obj_id());
		param.put("state", stateHisInfo.getState());
		param.put("total_fee", stateHisInfo.getTotal_fee());
		param.put("user_id", stateHisInfo.getUser_id());
		param.put("type", stateHisInfo.getType());
		param.put("desc", stateHisInfo.getDesc());
		param.put("update_by", stateHisInfo.getUpdate_by());
		yjxOrderDao.insertStateHisInfo(param);
	}
	
	// 用户通知
	public void addNotifyUserRecd(Map<String,Object> param){
		yjxOrderDao.insertNotifyUserlInfo(param);
	}

	public YjxOrdersDao getYjxOrderDao() {
		return yjxOrderDao;
	}

	public void setYjxOrderDao(YjxOrdersDao yjxOrderDao) {
		this.yjxOrderDao = yjxOrderDao;
	}

	public Page getPtsHisInfo(Map<String, Object> param, Integer pageNo,
			Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = yjxOrderDao.getPtsHisInfoCnt(param);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			param.put("pageSize", pageSize);
			param.put("skipResults", skipResults);
			List<YjxUserPointsHis> data = yjxOrderDao.getPtsHisInfo(param);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
			//clearSelectDataSource(dataSource);
		}
	}
}
